package com.yw.swagger.controller;

import com.yw.swagger.customNote.qw;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Global;
import com.yw.swagger.tool.Utils.HttpClientUtils;
import com.yw.swagger.tool.controller.BaseController;
import io.swagger.annotations.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: Abeiv
 * @Description:
 * @Date: 2019-10-29 11:45
 */
@Api(tags = "调用 webservice")
@Controller
@RequestMapping("/")
public class AmsController extends BaseController {

    @qw
    @ApiOperation(notes = "调用", value = "调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", example = "A8383U3243"),
            @ApiImplicitParam(name = "methodName", value = "WSDL里面描述的接口名称"),
            @ApiImplicitParam(name = "paramName", value = "参数列表 示例： name1,name2,nam3"),
            @ApiImplicitParam(name = "paramValue", value = "参数值列表 示例： value1,value2,value3")
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/request_service")
    @ResponseBody
    public AjaxResult requestService(String methodName, String paramName, String paramValue) {
        String endpoint = Global.getConfig("yw.webService");


        try {
            String[] paramNames = paramName.split(":::");
            String[] paramValues = paramValue.split(":::");
            if (paramNames.length != paramValues.length) {
                return AjaxResult.error("参数不正确");
            } else {
                Map map = new HashMap();
                for (int i = 0; i < paramNames.length; i++) {
                    map.put(paramNames[i], paramValues[i]);
                }
                System.err.println("map:"+map);
                HttpClientContext clientContext = HttpClientContext.create();
                String s = HttpClientUtils.httpPost(endpoint + "/" + methodName, map, 10000, clientContext);
                AjaxResult aj = new AjaxResult(AjaxResult.Type.SUCCESS, null);
                aj.put("data", s);
                return aj;
            }
        } catch (Exception e) {
            return AjaxResult.error("服务器运行错误");
        }
    }

}
